package com.common.operationLog.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class JsonFlatUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将JSON字符串转换为扁平化的Map
     * @param jsonStr JSON字符串
     * @return 扁平化的键值对Map
     * @throws Exception 如果JSON解析失败
     */
    public static Map<String, Object> flat(String jsonStr) throws Exception {
       JsonNode jsonNode = objectMapper.readTree(jsonStr);
       return flat(jsonNode);
    }

    /**
     * 将JSON字符串转换为扁平化的Map
     * @param jsonNode JSON节点
     * @return 扁平化的键值对Map
     * @throws Exception 如果JSON解析失败
     */
    public static Map<String, Object> flat(JsonNode jsonNode) {
        Map<String, Object> flatMap = new LinkedHashMap<>();
        flattenJson("", jsonNode, flatMap);
        return flatMap;
    }

    private static void flattenJson(String currentPath, JsonNode node, Map<String, Object> flatMap) {
        if (node.isObject()) {
            // 处理对象类型
            node.fields().forEachRemaining(entry -> {
                String key = entry.getKey();
                String newPath = currentPath.isEmpty() ? key : currentPath + "." + key;
                flattenJson(newPath, entry.getValue(), flatMap);
            });
        } else if (node.isArray()) {
            // 处理数组类型
            boolean containComplexField = false;
            for (JsonNode itemNode : node) {
                containComplexField = containComplexField || itemNode.isObject() || itemNode.isArray();
                if (containComplexField) {
                    break;
                }
            }

            if (containComplexField) {
                // 复杂的对象数组 或 数组嵌套 继续递归解析
                int index = 0;
                for (JsonNode itemNode : node) {
                    String newPath = currentPath + "[" + index + "]";
                    flattenJson(newPath, itemNode, flatMap);
                    index++;
                }
            } else {
                // 简单的数组 简化处理
                List<String> list = new ArrayList<>();
                for (JsonNode itemNode : node) {
                    Object value = getValue(itemNode);
                    list.add(String.valueOf(value));
                }
                flatMap.put(currentPath, "[" + String.join(",", list) + "]");
            }
        } else {
            // 处理基本类型
            Object value = getValue(node);
            flatMap.put(currentPath, value);
        }
    }

    private static Object getValue(JsonNode node) {
        if (node.isTextual()) {
            return node.textValue();
        } else if (node.isInt()) {
            return node.intValue();
        } else if (node.isLong()) {
            return node.longValue();
        } else if (node.isDouble()) {
            return node.doubleValue();
        } else if (node.isBoolean()) {
            return node.booleanValue();
        } else if (node.isNull()) {
            return null;
        } else {
            return node.toString();
        }
    }

}
